/**
 * Created by yangxianming on 2020/8/14.
 */
//
//接口定义 都是以api开头 所需参数都通过方法传入
//
var dir = "";   //当前选择目录
//加载目录
function apiGetDir(path){
    var data = {
        "action": "getDir",
        "path": path
    };

    var xmlHttp1 = new XMLHttpRequest();
    xmlHttp1.onreadystatechange = function(){
        if (xmlHttp1.readyState==4){
            if(xmlHttp1.status==200){
                var r = JSON.parse(xmlHttp1.response);
                if (r["apiStatus"] == "1"){
                    drawDirList(r["result"], path);
                }else{
                    alert(r["errorMsg"]);
                }
            }
        }
    };
    xmlHttp1.open("POST", "/video/videoFileManager", true);
    xmlHttp1.send(JSON.stringify(data));
}

//
//页面渲染 都是以draw开头
//
//绘制头部目录列表
var thisPath = "";
function drawDirList(dir, path){
    var divButtonList = document.getElementById("buttonList");
    var myTab = document.getElementById("myTab");
    myTab.innerText = "";
    path = path.replace(/\|/g, "/");
    divButtonList.innerText = "";
    for (var i = 0; i < dir["file"].length; i++){
        var aLi = document.createElement("li");
        var a1 = document.createElement("a");
        var url = "/ct/postgraduate/" + path + "/" + dir["file"][i];
        a1.setAttribute("target", "_blank");
        a1.setAttribute("target", "_blank");
        a1.setAttribute("href", url);
        a1.innerText = dir["file"][i];
        aLi.appendChild(a1);
        myTab.appendChild(aLi);
    }
    for (var i1 = 0; i1 < dir["dir"].length; i1++){
        var p1 = document.createElement("p");
        p1.style.cursor = "pointer";
        //用|符号分割后端会做替换，以适应不用环境
        p1.onclick = function(){thisPath = thisPath + this.innerText + "|";apiGetDir(thisPath)};
        p1.innerText = dir["dir"][i1];
        divButtonList.appendChild(p1);
    }
}

//初始化数据
function initialize(){
    apiGetDir("");
}